package code.untils
{
	import code.main.AppData;
	import code.tools.other.SecondDrive;
	
	import flash.utils.getTimer;
	
	public class SystemFunction
	{
		private static var _packId:int=50; //包ID
		private static var _cmdId:int;  //命令ID
		private static var _serverTime:int;
		private static var  _trueServerTime:int;
		/**
		 *服务器时区误差s 
		 */
		private static var _serverTimeOffset:int;
		/**
		 * 判断是否是同一天
		 * 毫秒级时间截
		 */		
		public static function isSameDay(day1:Number, day2:Number):Boolean
		{
			var _d1:Date = new Date(day1);
			var _d2:Date = new Date(day2);
			return _d1.fullYear == _d2.fullYear && _d1.month == _d2.month && _d1.date == _d2.date;
		}
		/**
		 * 获取当前时间 毫秒
		 */
		public static function get currentTime():String {
			return String(_serverTime * 1000);
		}
		
		/**
		 * 计算两天时间之间的天数间隔(1号-2号隔0天 1号-3号隔1天)
		 */		
		public static function countDeltaDay(value:int, value2:int = 0):int{
			if(value2 == 0) value2 = trueServerTime;
			var _d1:Date = new Date(value2 * 1000);
			var _d2:Date = new Date((value-_serverTimeOffset) *1000);
			_d1 = new Date(_d1.fullYear, _d1.month, _d1.date);
			_d2 = new Date(_d2.fullYear, _d2.month, _d2.date);
			
			return (_d1.time - _d2.time)/86400000 -1;
		}
		/**
		 * 是否和服务器同一天
		 */		
		public static function isSameDayWithServer(value:int):Boolean
		{
			var _d1:Date = new Date(trueServerTime * 1000);
			var _d2:Date = new Date((value-_serverTimeOffset) *1000);
			return (_d1.fullYear == _d2.fullYear && _d1.month == _d2.month && _d1.date == _d2.date);
		}
		/**
		 *服务器运行 
		 * 
		 */
		private static function serverTimerHandler():void{
			_serverTime++;
		}
		
		/**
		 * 获取距离下一天0点时间差
		 */
		public static function get zeroDeltaTime():int 
		{
			var d:Date = new Date(SystemFunction.trueServerTime*1000);
			var deltaT:int = 86400  - d.hours * 3600 - d.minutes * 60 - d.seconds;
			return deltaT;
		}
		/**
		 * 获取包ID
		 */
		public static function get packId():int {
			var _temp:int = getTimer();
			if (_temp <= _packId) 
				_packId += 1;
			else
				_packId = _temp;
			return _packId;
		}
		/**
		 * 获取命令ID
		 */
		static public function get cmdId():int 
		{
			return _cmdId++;
		}

		/**
		 * 当前的时区换算真实的服务器时间戳 s 
		 * 传入Date 进行运算
		 */
		public static function get trueServerTime():int
		{
			return serverTime - _serverTimeOffset;
		}
		/**
		 * 当前服务器时间 秒(严禁传入Date)
		 * (用来做加减法活动开启时间判断)
		 */
		public static function get serverTime():int {
			return _serverTime;
		}
		/**
		 *设置服务器时间 
		 * @param serverNum 服务器给的时间戳 s
		 * @param serverStr  服务器给的时间字符串 2013/03/14 18:23:59
		 * 
		 */

		public static function setServerTime(serverNum:int,serverStr:String,offsetN:int=0):void{
			var sD:Date = new Date(serverStr);
			_serverTimeOffset =  serverNum -  sD.time/1000 ;
			startSecondTime = serverNum + offsetN;
		}
		
		/**
		 * 初始化服务器时间（s）
		 * @param value
		 * 
		 */
		private static function set startSecondTime(value:int):void{
			_serverTime = value;
			if(!SecondDrive.hasEventListener(serverTimerHandler))
				SecondDrive.addEventListener(serverTimerHandler);
		}
	}
}